package com.ruoyi.project.notice.type.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.notice.type.domain.NoticeType;
import com.ruoyi.project.notice.type.service.NoticeTypeService;
import com.ruoyi.project.system.post.domain.Post;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/notice/type")
public class NoticeTypeController extends BaseController {

     String prefix = "notice/type";

     @Autowired
     private NoticeTypeService noticeTypeService;

    @RequiresPermissions("cms:notice:list")
    @GetMapping("")
    public String operlog()
    {
        return prefix + "/show";
    }

    @RequiresPermissions("cms:notice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(NoticeType noticeType)
    {
        startPage();
        List<NoticeType> list = noticeTypeService.findAll(noticeType);
        return getDataTable(list);
    }


    /**
     * 新增公告类别
     */
    @RequiresPermissions("cms:notice:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @RequiresPermissions("cms:notice:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated NoticeType noticeType)
    {
        if (UserConstants.TYPE_NAME_NOT_UNIQUE.equals(noticeTypeService.checkTypeNameUnique(noticeType)))
        {
            return error("新增岗位'" + noticeType.getTname() + "'失败，公告类别名称已存在");
        }
        return toAjax(noticeTypeService.insertPost(noticeType));
    }

    /**
     * 修改公告类型
     */
    @RequiresPermissions("cms:notice:edit")
    @GetMapping("/edit/{tid}")
    public String edit(@PathVariable("tid") Long tid, ModelMap mmap)
    {
        mmap.put("type", noticeTypeService.selectTypeById(tid));
        return prefix + "/edit";
    }


    /**
     * 修改保存公告类别
     */
    @RequiresPermissions("cms:notice:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated NoticeType noticeType)
    {

        return toAjax(noticeTypeService.updateType(noticeType));
    }


    @RequiresPermissions("cms:notice:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(noticeTypeService.deletePostByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }










    /**
     * 校验岗位名称
     */
    @PostMapping("/checkTypeNameUnique")
    @ResponseBody
    public String checkTypeNameUnique(NoticeType noticeType)
    {
        return noticeTypeService.checkTypeNameUnique(noticeType);
    }

}
